﻿using com.haoest.Utils.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace com.haoest.QrPortal
{
    public class Command
    {
        private static SimpleDatabase simpleDatabase = new SimpleDatabase("settings.db");  //记得sd.Close();

        public static void Disconnect(string deviceId)
        {
            foreach (Device device in Device.devices)
            {
                if (device.Id == deviceId)
                {
                    device.Disconnected += ConnectedDevicesList.Instance.RefreshConnectedDevicesCheckedListBox;
                    device.Disconnect();
                    break;
                }
            }
        }

        public static void AddDevice(string deviceId, string deviceName)
        {
            Device device = new Device(deviceId, deviceName);
            device.Connected += ConnectedDevicesList.Instance.RefreshConnectedDevicesCheckedListBox;
            device.Connect();
        }

        public static void RenameDevice(string deviceId, string deviceName)
        {
            foreach (Device device in Device.devices)
            {
                if (device.Id == deviceId)
                {

                    device.Renamed += ConnectedDevicesList.Instance.RefreshConnectedDevicesCheckedListBox;
                    device.Rename(deviceName);
                    break;
                }

            }
        }


        public static void OpenPortal()
        {

        }

        /// <summary>
        /// 获得数据库中的端口号
        /// </summary>
        /// <returns></returns>
        public static int GetPort()
        {
            try
            {
                int port = simpleDatabase.GetIntValue("port");
                return port;
            }
            catch { return 21345; }//默认是21345
        }

        /// <summary>
        /// 获取数据库中的SSID
        /// </summary>
        /// <returns></returns>
        public static string GetSsid()
        {
            return simpleDatabase.GetStringValue("ssid");
        }

        /// <summary>
        /// 获取数据库中的KEY
        /// </summary>
        /// <returns></returns>
        public static string GetKey()
        {
            return simpleDatabase.GetStringValue("key");
        }

        /// <summary>
        /// 保存SSID
        /// </summary>
        /// <param name="ssid"></param>
        public static void SaveSsid(string ssid)
        {
            simpleDatabase.SetValue("ssid", ssid);
        }

        /// <summary>
        /// 保存KEY
        /// </summary>
        /// <param name="key"></param>
        public static void SaveKey(string key)
        {
            simpleDatabase.SetValue("key", key);
        }
    }
}
